package ua.kharkov.knure.dereza.summarytask4.command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ua.kharkov.knure.dereza.summarytask4.core.Constants;
import ua.kharkov.knure.dereza.summarytask4.db.bean.ClientBean;

/**
 * Logout command
 * 
 * @author dereza
 *
 */
public class LogoutCommand extends Command{

	private static final long serialVersionUID = 4353667520309039114L;
	
	private static final Logger log = Logger.getLogger(LogoutCommand.class);
	
	@Override
	public String execute(HttpServletRequest req, HttpServletResponse res) {
        log.debug("Command starts");
		HttpSession session = req.getSession(false);
		ClientBean clientBean = (ClientBean)session.getAttribute("clientbean");
		if (session != null && clientBean != null){
			String client = clientBean.getLogin();
			log.info("Logged out client --> " + client);
			session.invalidate();
		}		
		log.debug("Command finished");
		String forward = Constants.PAGE_LOGIN;
		return forward;
	}

	@Override
	public String toString() {
		return "LogoutCommand";
	}	
}